$(function() {
//报废
{if $Think.get.car_audit_id}
$('.audit_ok').on('click',function(){
    $.ajax({
        type: "post",
        url: "{:url('admin/car/audit_ok')}",
        data: {
            car_audit_id:{$Think.get.car_audit_id}
        },
        success: function(res) {
            Load(1);
            console.log(res)
            if(res.code==1){
                Msg('车辆确认')
                setTimeout(function(){
                    window.history.back();
                },1000)
            }else{
                Err('请求失败')
            }
        }
    })
	// Msg('车辆确认')
	// setTimeout(function(){
	// window.history.back();
	// },1000)
})
{/if}
$('.audit_no').on('click',function(){
	window.history.back();
})
$('.blank_close').on('click',function(){
	window.close()
})
$('.scrapok').on('click',function(){
	Msg('同意报废')
	window.location.href="{:url('scrap')}"
})
$('.scrapno').on('click',function(){
	Msg('拒绝报废')
	window.location.href="{:url('scrap')}"
})
//报废结束
	var search = window.location.search
	var arr;
	if(search) {
		search = search.substring(1)
		arr = search.split('=')
		if(arr[0] == 'service') {
			$('.am-nav-tabs li').removeClass('am-active')
			$('.am-nav-tabs li').eq(1).addClass('am-active')
			$('.am-tabs-bd-ofv>div').removeClass('am-active')
			$('.am-tabs-bd-ofv>div').eq(1).addClass('am-active')
		} else if(arr[0] == 'refuel') {
			$('.am-nav-tabs li').removeClass('am-active')
			$('.am-nav-tabs li').eq(2).addClass('am-active')
			$('.am-tabs-bd-ofv>div').removeClass('am-active')
			$('.am-tabs-bd-ofv>div').eq(2).addClass('am-active')
		} else if(arr[0] == 'insure') {
			$('.am-nav-tabs li').removeClass('am-active')
			$('.am-nav-tabs li').eq(3).addClass('am-active')
			$('.am-tabs-bd-ofv>div').removeClass('am-active')
			$('.am-tabs-bd-ofv>div').eq(3).addClass('am-active')
		} else if(arr[0] == 'survey') {
			$('.am-nav-tabs li').removeClass('am-active')
			$('.am-nav-tabs li').eq(4).addClass('am-active')
			$('.am-tabs-bd-ofv>div').removeClass('am-active')
			$('.am-tabs-bd-ofv>div').eq(4).addClass('am-active')
		}
	}
	$('.back').on('click', function() {
		window.history.back();
	});
	$('.service_detail').on('click',function(){
		var that=this
		Load();
		$.ajax({
			type: "post",
			url: "{:url('admin/service/read')}",
			data: {
				maintenance_record_id:$(this).closest('tr').data('id')
			},
			success: function(res) {
				Load(1);
				console.log(res)
				if(res.code==1){
					var keyname=['维保单号','车牌号','维保类型','维保金额','送维/送保人','维保公司','开始时间','结束时间','维保公司联系人','维保公司联系电话','地点','备注说明'];
					var keyvalue=['maintenance_record_id','number','service_type','price','leader','service_company','start_date','end_date','service_linkman','service_phone','address','ps']

					res.data.img = fixImgUrl(res.data.service_img);

					var service_detail=`${detailmain('维保详情',res.data,keyname,keyvalue)}`;
					var index = layer.open({
						title: false,
						type: 1,
						skin: 'layui-layer-rim', //加上边框
						area: ['400px', '70%'], //宽高
						content:service_detail,
						success: function(res) {
							console.log(res)
						}
					})
				}else{
					Err('请求失败')
				}
			}
		})


	});
	$('.refuel_detail').on('click',function(){
		var that=this
		console.log($(this).closest('tr').data('id'))
		Load();
		$.ajax({
			type: "post",
			url: "{:url('admin/refuel/read')}",
			data: {
				refuel_record_id:$(this).closest('tr').data('id')
			},
			success: function(res) {
				console.log(res)
				Load(1);
				if(res.code==1){
					var keyname=['加油编号','车牌号','司机','加油员','加油油号','手机号','加油量','加油金额','加油时间','车辆公里数','加油站','加油地址'];
					var keyvalue=['refuel_record_id','car_number','name','fuel_man','label','phone','litre','price','start_date','km','gas_station','refuel_add']

					res.data.img = fixImgUrl(res.data.img);
					var refuel_detail=`${detailmain('加油详情',res.data,keyname,keyvalue)}`;
					var index = layer.open({
						title: false,
						type: 1,
						skin: 'layui-layer-rim', //加上边框
						area: ['400px', '70%'], //宽高
						content:refuel_detail,
						success: function(res) {
							console.log(res)
						}
					})
				}else{
					Err('请求失败')
				}
			},

		})
	});
	$('.insure_detail').on('click',function(){
		var that=this
		Load();
		$.ajax({
			type: "post",
			url: "{:url('admin/insure/read')}",
			data: {
				car_insure_id:$(this).closest('tr').data('id')
			},
			success: function(res) {
				Load(1);
				console.log(res)
				if(res.data.policy_type==1){
					res.data.policytype='维修'
				}else{
					res.data.policytype='保养'
				}
				if(res.code==1){
					var keyname=['保单号','车牌号','保险种类','购买金额','保险公司','投保人','保险公司联系人','保险公司联系电话','投保时间','到期时间','备注说明'];
					var keyvalue=['policy_number','number','policytype','policy_charge','insurance_company','policy_holder','linkman','phone','start_date','end_date','ps']

					res.data.img = fixImgUrl(res.data.policy_img);
					var insure_detail=`${detailmain('保险详情',res.data,keyname,keyvalue)}`;
					var index = layer.open({
						title: false,
						type: 1,
						skin: 'layui-layer-rim', //加上边框
						area: ['400px', '70%'], //宽高
						content:insure_detail,
						success: function(res) {
							console.log(res)
						}
					})
				}else{
					Err('请求失败')
				}
			}
		})

	});
	$('.survey_detail').on('click',function(){
		var that=this
		Load();
		$.ajax({
			type: "post",
			url: "{:url('admin/survey/read')}",
			data: {
				mot_record_id:$(this).closest('tr').data('id')
			},
			success: function(res) {
				Load(1);
				console.log(res)
				console.log(res.data.state)
				if(res.data.state==1){
					res.data.mot_state='通过'
				}else{
					res.data.mot_state='不通过'
				}
				if(res.code==1){
					var keyname=['车牌号','年检地址','当年年检时间','下次年检时间','送检人','年检费用','检验员','年检状态','年检公司','年检联系电话','备注说明'];
					var keyvalue=['number','mot_add','start_date','end_date','sender','price','checker','mot_state','company','phone','ps'];

					res.data.img = fixImgUrl(res.data.img);
					var survey_detail=`${detailmain('年检详情',res.data,keyname,keyvalue)}`;
					var index = layer.open({
						title: false,
						type: 1,
						skin: 'layui-layer-rim', //加上边框
						area: ['400px', '70%'], //宽高
						content:survey_detail,
						success: function(res) {
							console.log(res)
						}
					})
				}else{
					Err('请求失败')
				}
			}
		})

	});

	$.ajax({
			type: "post",
			url: "{:url('admin/beidou/cars')}",
			data: {
				pagesize: 10,
				number:"{$item.number}"
			},
			success: function(res) {
				console.log(res.data)
				if(res.data.length!=0){
					AMap.plugin('AMap.Geocoder', function() {
						geocoder = new AMap.Geocoder({
							city: "010" //城市，默认：“全国”
						});
						geocoder.getAddress(res.data[0].loc, function(status, result) {
							if(status === 'complete' && result.info === 'OK') {
								//获得了有效的地址信息:
								//即，
								if(result.regeocode.formattedAddress==""){
									$('.loc').html('无定位信息')
								}else{
									$('.loc').html(result.regeocode.formattedAddress)
								}
								console.log(result.regeocode.formattedAddress)
							} else {
								$('.loc').html('无定位信息')

							}
						});
						$(that).on('mouseout', function() {
							layer.close(tips)
						})
					})

				}else{
					$('.loc').html('无定位信息')
				}
			}
	})
	//title ...的详情
	//keyname 详情key中文
	//detail 详情内容
	//keyvalue 详情key
	function detailmain(title,detail,keyname,keyvalue){
		var detailma=`<div class="am-u-sm-12" style="padding:20px">
								<div class="am-u-sm-12 cl-p">
									<fieldset class="cl-p cl-m">
										<legend>${title}</legend>
									</fieldset>
								</div>
								<div class="am-u-sm-12 cl-p">
									<div class="am-u-sm-12" style='padding-top:10px'>`;
		for(var i=0;i<keyvalue.length;i++){
			if(keyvalue[i]=='label'){
				detailma+=` <div class="am-u-sm-12 cl-p order_detail_text">
							<div class="detail-list1">${keyname[i]}:</div>#${detail[keyvalue[i]]}
						</div>`
			}else{
			detailma+=` <div class="am-u-sm-12 cl-p order_detail_text">
							<div class="detail-list1">${keyname[i]}:</div><div class='am-fl' style='width:150px'>${detail[keyvalue[i]]}</div>
						</div>`
			}
		}

		if(detail.img){
            detailma+=`<img src='${detail.img}' alt="图片" style='width:200px;height:100px;margin-left:20px'/>`
		}

		detailma+=`</div>
					</div>
				</div>`
		return detailma
	}

	// 修复图片路径
	function fixImgUrl(url) {
        return url ? (url[0] == '/' ? url : '/upload/' +  url) : null;
	}
})